package org.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.entity.SysMenu;
import org.example.mapper.MenuMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DeleteMenuByIdService {

    @Resource
    private MenuMapper menuMapper;

    public void deleteMenuById(Integer id){

        ArrayList<Integer> array=new ArrayList<>();
        array.add(id);
        getIds(array,id);

        menuMapper.deleteBatchIds(array);


    }

    private void getIds(ArrayList<Integer> array,Integer id){

        QueryWrapper<SysMenu> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<SysMenu> twoSubjects = menuMapper.selectList(wrapper);

        for(SysMenu sysMenus:twoSubjects){
            Integer childId = sysMenus.getId();
            array.add(childId);
            this.getIds(array,childId);

        }

    }

}
